package com.ruoyi.vote.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.vote.domain.Options;
import com.ruoyi.vote.service.IOptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 选项信息Controller
 *
 * @author wxx
 * @date 2022-10-05
 */
@Controller
@RequestMapping("/vote/options")
public class OptionsController extends BaseController {
    private String prefix = "vote/options";

    @Autowired
    private IOptionsService optionsService;

    @GetMapping()
    public String options() {
        return prefix + "/options";
    }

    /**
     * 查询选项信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Options options) {
        startPage();
        List<Options> list = optionsService.selectOptionsList(options);
        return getDataTable(list);
    }

    @GetMapping("/list/{voteId}")
    @ResponseBody
    public List<Options> find(@PathVariable Long voteId) {
        return optionsService.selectOptionsByVoteId(voteId);
    }

    @GetMapping("/list")
    @ResponseBody
    public AjaxResult listByVoteId(Long voteId) {
        Options options = new Options();
        options.setVoteId(voteId);
        List<Options> list = optionsService.selectOptionsList(options);
        return list != null ? AjaxResult.success("查询成功", list) : AjaxResult.error("查询失败");
    }

    /**
     * 新增选项信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存选项信息
     */
    @Log(title = "选项信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Options options) {
        return toAjax(optionsService.insertOptions(options));
    }

    @PostMapping("/add1/{voteId}")
    @ResponseBody
    public AjaxResult add1(@PathVariable Long voteId, @RequestBody String[] ops) {
        boolean flag = true;
        for (String op : ops) {
            Options options = new Options();
            options.setVoteId(voteId);
            options.setOption(op);
            int i = optionsService.insertOptions(options);
            if (i <= 0) {
                flag = false;
            }
        }
        return flag ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }

    /**
     * 修改选项信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Options options = optionsService.selectOptionsById(id);
        mmap.put("options", options);
        return prefix + "/edit";
    }

    /**
     * 修改保存选项信息
     */
    @Log(title = "选项信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Options options) {
        return toAjax(optionsService.updateOptions(options));
    }

    @PostMapping("/edit1")
    @ResponseBody
    public AjaxResult edit1(@RequestBody Options[] options) {
        boolean flag = true;
        for (Options option : options) {
            if (option.getId() == null) {
                int i = optionsService.insertOptions(option);
                if (i <= 0) {
                    flag = false;
                }
            } else {
                int i = optionsService.updateOptions(option);
                if (i <= 0) {
                    flag = false;
                }
            }

        }
        return flag ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除选项信息
     */
    @Log(title = "选项信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(optionsService.deleteOptionsByIds(ids));
    }
}
